import {a_soldier_actionproperty, a_soldier_actions} from "../commdata";
import {Fighter} from "./fighter";
import g1 from "../sound/g1.wav";

export class A_soldier {
    name: string = ""
    fun: string = "l"  //"l" "r" "u" "d" "oul" "our" "odl" "odr"
    live: number = 100;
    livelimit: number = 100;
    x: number = 0;
    y: number = 0;
    demage: number = 5;
    actionindex: number = a_soldier_actionproperty.actionindex;
    actioncount: number = a_soldier_actionproperty.actioncount;
    speed: number = 5;
    team: number = 0;
    actionimg: any;
    actrons: any = a_soldier_actions;
    currantactions:any=[];
    ratewidth:number=a_soldier_actionproperty.ratewidth;
    rateheight:number=a_soldier_actionproperty.rateheight;
    imgyy:any;
    deadimg:any;
    isfire:boolean=false;
    sdadio=new Audio();

    constructor(_name:string,_live:number,_livelimit:number,_x:number,_y:number) {

        this.name=_name;
        this.live=_live;
        this.livelimit=_livelimit;
        this.x=_x;
        this.y=_y;
        this.setaction();
        this.currantactions=[];
        this.actionimg=new Image();
        this.imgyy=new Image();
        this.imgyy.src=a_soldier_actionproperty.yyimgsrc;
        this.deadimg=new Image();
        this.deadimg.src= a_soldier_actionproperty.dead1;
        this.sdadio.src=g1;
        for(let i=0;i<this.actrons.length;i++)
        {
            if(this.fun==this.actrons[i].fun) {
                let temp = this.actrons[i];
                this.currantactions.push(temp);
            }
        }
    }


    actiondraw = (ptctx: any, max_width: number, max_height: number) => {
        if(this.live>0) {
            if (this.fun == "l") {
                this.x = this.x - this.speed;
            } else if (this.fun == "r") {
                this.x = this.x + this.speed;
            } else if (this.fun == "u") {
                this.y = this.y - this.speed;
            } else if (this.fun == "d") {
                this.y = this.y + this.speed;
            } else if (this.fun == "oul") {
                this.x = this.x - this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "our") {
                this.x = this.x + this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "odl") {
                this.x = this.x - this.speed;
                this.y = this.y + this.speed;
            } else if (this.fun == "odr") {
                this.x = this.x + this.speed;
                this.y = this.y + this.speed;
            } else {
                this.x = this.x + this.speed;
            }

            if (this.x < 0) {
                this.actionindex = 0;
                this.fun = "r";
                this.x = 0;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.x > max_width - this.ratewidth) {
                this.actionindex = 0;
                this.fun = "l";
                this.x = max_width - this.ratewidth;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y < 0) {
                this.actionindex = 0;
                this.y = 0;
                this.fun = "d";
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y > max_height - this.rateheight) {
                this.actionindex = 0;
                this.fun = "u";
                this.y = max_height - this.rateheight;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            }
            this.currantactions = [];
            for (let i = 0; i < this.actrons.length; i++) {
                if (this.fun == this.actrons[i].actiontype) {
                    let temp = this.actrons[i];
                    this.currantactions.push(temp);
                }
            }
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x, this.y, this.ratewidth, this.rateheight
            );
            if (this.actionindex < this.actioncount - 1)
                this.actionindex = this.actionindex + 1;
            else
                this.actionindex = 0;
        }
    }

    drawshowdo=(ptctx: any)=>{
        if(this.live>0) {
            //绘制阴影
            ptctx.drawImage(this.imgyy, 0, 0, this.imgyy.width, this.imgyy.height, this.x + 5, this.y + this.rateheight - 15, this.ratewidth - 15, this.rateheight / 2 - 15);
        }
    }

    deaddraw=(ptctx: any)=>{
        if(this.live<=0) {
            //死亡
            if (this.fun == "l" || this.fun == "oul" || this.fun == "odl") {
                this.actionimg.src = a_soldier_actionproperty.dead1

            } else {
                this.actionimg.src = a_soldier_actionproperty.dead2
            }
            ptctx.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x-20, this.y, this.actionimg.width, this.actionimg.height);

        }
    }

    fire=(fighter:Fighter)=>{
        let flagnum: number = Math.floor(Math.random() * (100 - 0 + 1)) + 0;
        if(flagnum>85 && flagnum < 95)
        {
            this.isfire=true;
            //射中机率
            let tmpflag = Math.floor(Math.random() * (70 - 0 + 1)) + 0;
            if (tmpflag > 50) {
               // this.sdadio.play();
                fighter.live = fighter.live - this.demage;
            }
        }
    }

    setaction=()=>{
        this.isfire=false;
        if(this.live>0) {
            let flagnum: number = Math.floor(Math.random() * (200 - 0 + 1)) + 0;
            if (flagnum > 10 && flagnum < 13) {
                this.actionindex = 0;
                this.fun = "u";
            } else if (flagnum > 30 && flagnum < 33) {
                this.actionindex = 0;
                this.fun = "d";
            } else if (flagnum > 55 && flagnum < 58) {
                this.actionindex = 0;
                this.fun = "l";
            } else if (flagnum > 70 && flagnum < 73) {
                this.actionindex = 0;
                this.fun = "r";
            } else if (flagnum > 100 && flagnum < 103) {
                this.actionindex = 0;
                this.fun = "oul";
            } else if (flagnum > 110 && flagnum < 113) {
                this.actionindex = 0;
                this.fun = "our";
            } else if (flagnum > 130 && flagnum < 133) {
                this.actionindex = 0;
                this.fun = "odr";
            } else if (flagnum > 180 && flagnum < 184) {
                this.actionindex = 0;
                this.fun = "odl";
            }

        }
    }
}